package com.hsd.core.tags;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;

import com.hsd.core.cache.DictCache;
import com.hsd.core.tags.base.AbstarctHtmlTag;
import com.hsd.core.tags.helper.OptionWriter;
import com.hsd.core.tags.helper.TagWriter;

/**
 * 下拉标签,支持代码集
 * 
 * @author Abdi
 * @description
 * @created Oct 25, 2013 1:38:04 PM
 */
@SuppressWarnings("unchecked")
public class SelectTag extends AbstarctHtmlTag {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6928709413729387843L;
	private Object multiple = Boolean.FALSE;
	private String size;
	private String valueset;
	private String selectedValue; // 选中值，对应option将被指定
	private Object hideEmpty; // 是否隐藏空值，默认false
	private String params; // 自定义条件

	/**
	 * 输出SELECT标签
	 * 
	 * @param tagWriter
	 * @return
	 * @throws JspException
	 */
	@Override
	protected int writeTagContent(TagWriter tagWriter) throws JspException {
		tagWriter.startTag("select");
		writeDefaultAttributes(tagWriter);
		if (isMultiple()) {
			tagWriter.writeAttribute("multiple", "multiple");
		}
		tagWriter.writeOptionalAttributeValue("size",
				getDisplayString(evaluate("size", getSize())));
		// 根据代码值输出option
		if (null != getValueset()) {
			Map queryMap = new HashMap();
			String params = getDisplayString(evaluate("params", getParams()));
			DictCache cache = DictCache.getInstance();
			List<Map> optionSource = null;
			if(null != cache){
				optionSource = cache.getCodeList(
						getValueset(), queryMap);
			}
			OptionWriter optionWriter = new OptionWriter(optionSource, getDisplayString(evaluate(
					"selectedValue", getSelectedValue())), evaluateBoolean(
					"hideEmpty", (String) hideEmpty));
			optionWriter.writeOptions(tagWriter);
		}
		tagWriter.endTag(true);
		return SKIP_BODY;
	}

	/**
	 * 判断是否多选
	 * 
	 * @return
	 * @throws JspException
	 */
	private boolean isMultiple() throws JspException {
		Object multiple = getMultiple();
		if (Boolean.TRUE.equals(multiple) || "multiple".equals(multiple)) {
			return true;
		} else if (this.multiple instanceof String) {
			return evaluateBoolean("multiple", (String) multiple);
		}
		return false;
	}

	public Object getHideEmpty() {
		return hideEmpty;
	}

	public void setHideEmpty(Object hideEmpty) {
		this.hideEmpty = hideEmpty;
	}

	public Object getMultiple() {
		return multiple;
	}

	public void setMultiple(Object multiple) {
		this.multiple = multiple;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public String getValueset() {
		return valueset;
	}

	public void setValueset(String valueset) {
		this.valueset = valueset;
	}

	public String getSelectedValue() {
		return selectedValue;
	}

	public void setSelectedValue(String selectedValue) {
		this.selectedValue = selectedValue;
	}

	public String getParams() {
		return params;
	}

	public void setParams(String params) {
		this.params = params;
	}
}
